﻿// ActionScript file
/* 
	@子类重载drawShape
	@setLineStyle & setFillStyle 设置线条、填充的样式�?
 */
package org.nickro.graphics{
	import flash.display.Shape;
	import flash.events.Event;
	
	public class BasicShape extends Shape{
		private var lineStyle:Array=new Array;
		private var fillStyle:Array=new Array;
		private var changed:Boolean=false;
		private var colorFilled:Boolean=false;
		public function BasicShape(){
			addEventListener(Event.ADDED_TO_STAGE,onAdded);
			addEventListener(Event.REMOVED_FROM_STAGE,onRemoved);
		}
/* --------------------------------------------
		public Methods
	----------------------------------------- */	
		public function setLineStyle(thickness:Number=0,color:uint=0,alpha:Number=1):void{
			lineStyle.push(thickness,color,alpha);
			setChanged();
		}
		public function setFillStyle(color:uint=0,alpha:Number=1):void{
			fillStyle.push(color,alpha);
			setChanged();
			colorFilled=true;
			
		}
/* 	----------------------------------	
		Event Handlers
		
	----------------------------------- */	
		private function onAdded(e:Event):void{
			stage.addEventListener(Event.RENDER,onRender);
			if(hasChanged()){
				requestDraw();
			}
		}
		private function onRemoved(e:Event):void{
			stage.addEventListener(Event.RENDER,onRender);
		}
		private function onRender(e:Event):void{
			if(hasChanged()){
				draw();
			}
		}
		private function requestDraw():void{
			if(stage!=null){
				stage.invalidate();
			}
		}
		private function hasChanged():Boolean{
			return changed;
		}
		private function setChanged():void{
			changed=true;
			requestDraw();
		}
		
		
		//绘制
		private function draw():void{
			this.graphics.lineStyle(lineStyle[0],lineStyle[1],lineStyle[2]);
			this.graphics.beginFill(fillStyle[0],fillStyle[1]);
			drawShape();
			this.graphics.endFill();
			changed=false;
		}
		protected function drawShape():void{
			throw new Error("只能对子项执�?);
		}
		
	}
}